import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 提交表单列表
		formData : [
			{ label: '选项',		name: 'num_key', 	full:true, required:true, type: 'select', loading:true, onValue:({ rows })=>{
				this.submit.option = rows?.name
			} },
			{ label: '结果',		name: 'result', 	full:true },
			{ label: '注释',		name: 'notes', 		full:true },
			{ label: '排序',		name: 'sort', 		full:true, type:'number'  },
		]
	}
	
	model = {}
	submit = {}
	
	componentDidMount(){
		
		this.fetch()
	}
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'num_key', ()=>{
			$cache.then( async f => {
				// 选项
				$fn.setRow(formData, 'num_key',  await f.getCache({ api:'conftctSelect'}))
				//
				this.setState({ formData })
			})
		})
	}
	// paging
	fetch = () => $fn.fetch.call(this,'conf-tct/index')
	
	// 表单
	cols = [
		{ title: '选项', 	dataIndex: 'option', 			width:200 },
		{ title: '结果', 	dataIndex: 'result', 			width:200 },
		{ title: '注释', 	dataIndex: 'notes', 			width:400 },
		{ title: '数值', 	dataIndex: 'num_key', 			width:100 },
		{ title: '排序', 	dataIndex: 'sort', 				...$fn.table.num },
		TableControls.controls.call(this,{width:100})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'conf-tct/info', { uuid }, {
				select: this.getModalSelect
			}),										// 编辑
			this.onDel([ uuid ]),					// 删除
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this,{click:this.getModalSelect, code:false}),			// 添加
			this.onDel(),																// 删除
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'conf-tct/del', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='TCT规则设置' controls={this.controls()}>
					{/* 表格 */}
					{ Table.call(this,{wrapClassName:'mt10 mlr10'}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'conf-tct/add', editApi:'conf-tct/edit' }) }
			</>
		)
	}
}

export default Index